package com.proactive.webapp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.DesignCollectDAO;
import com.proactive.webapp.dao.MemberDAO;
import com.proactive.webapp.dao.MenuDAO;
import com.proactive.webapp.entity.Design;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.Menu;
import com.proactive.webapp.util.MenuUtil;
import com.proactive.webapp.util.PageCount;

@Controller
public class DesignCollectController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");

	@RequestMapping(value = "/collectDesign", method = RequestMethod.GET)
	public ModelAndView getCollectDesignList(HttpServletRequest request, HttpSession session) {
		ModelAndView model = new ModelAndView();
		Member loginMember = (Member)session.getAttribute("loginMember");
		if (request.getParameter("id") != null) {
			MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");
			DesignCollectDAO designCollectDAO = (DesignCollectDAO) context.getBean("designCollectDAO");
			Member member = new Member();			
			List<Design> designList = new ArrayList<Design>();
			String loginAccount = "";
			
			String memberNO = request.getParameter("id");
			int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
			
			member.setMemberNO(memberNO);
			member = memberDAO.getByMemberNO(member);//用memberNO取得memberAccount
			if(member.getAccount()!=null){
				
				if(loginMember != null){
					member = memberDAO.getByLogin(member, loginMember.getAccount());//取得member資料
					loginAccount = loginMember.getAccount();
				}
				
				
				//set pager
				int count = designCollectDAO.countTotal(member.getAccount());
				int pageSize = 12;
				String pageUrl = "collectDesign";
				String pageEndUrl1 = "&id="+memberNO;
				PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl1);
				pageCount.calculate();
				
				designList = designCollectDAO.getList(loginAccount, member.getAccount(), pageCount.getStart(), pageSize);
				
				List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 0);
				
				//set model
				model.addObject("page", pageCount);
				model.addObject("member", member);
				model.addObject("designList", designList);
				model.addObject("menuList", menuList);
				model.setViewName(loginMember!=null?"collectDesign1":"collectDesign2");
			
			}
			else
				model.setViewName("redirect:/");
			
		} 
		else
			model.setViewName(loginMember!=null?"redirect:/collectDesign?id="+loginMember.getMemberNO():"redirect:/login");

		
		return model;
	}
}
